<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <!-- 查询表单 -->
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-position="top">
          <el-form-item label="资源名称">
            <el-input v-model="listQuery.name" class="input-width" placeholder="资源名称" clearable></el-input>
          </el-form-item>
          <el-form-item label="资源路径">
            <el-input v-model="listQuery.url" class="input-width" placeholder="资源路径" clearable></el-input>
          </el-form-item>
          <el-form-item label="资源分类">
            <el-select v-model="listQuery.categoryId" placeholder="全部" clearable class="input-width">
              <el-option v-for="item in categoryOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="相关操作">
            <el-button type="infor" @click="handleResetSearch()" size="small">重置条件</el-button>
            <el-button type="primary" @click="handleSearchList()" size="small">查询搜索</el-button>
            <el-button type="success" @click="handleAdd()" size="small">添加资源</el-button>
            <el-button type="warning" @click="handleShowCategory()" size="small">资源分类</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <div class="table-container">
      <el-table ref="resourceTable" :data="list" style="width: 100%;" v-loading="listLoading" border>
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>
        <el-table-column label="资源名称" align="center">
          <template slot-scope="scope">{{scope.row.name}}</template>
        </el-table-column>
        <el-table-column label="资源路径" align="center">
          <template slot-scope="scope">{{scope.row.url}}</template>
        </el-table-column>
        <el-table-column label="描述" align="center">
          <template slot-scope="scope">{{scope.row.description}}</template>
        </el-table-column>
        <el-table-column label="添加时间" width="180" align="center">
          <template slot-scope="scope">{{scope.row.createdTime | formatDateTime}}</template>
        </el-table-column>
        <el-table-column label="操作" width="140" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination-container" style="margin-top: 0.625rem;">
      <el-pagination background @size-change="handlePageSizeChange" @current-change="handleCurrentPageChange" layout="total, sizes,prev, pager, next,jumper"
        :current-page="listQuery.currentPage" :page-sizes="[5,10, 20]" :page-size="listQuery.pageSize" :total="total"></el-pagination>
    </div>

    <!-- 添加&修改表单 -->
    <el-dialog :title="isEdit?'编辑资源':'添加资源'" :visible.sync="dialogVisible" width="40%">
      <el-form :model="resource" ref="resourceForm" label-width="150px" size="small">
        <el-form-item label="资源名称：">
          <el-input v-model="resource.name" style="width: 250px"></el-input>
        </el-form-item>
        <el-form-item label="资源路径：">
          <el-input v-model="resource.url" style="width: 250px"></el-input>
        </el-form-item>
        <!-- 下拉列表 -->
        <el-form-item label="资源分类：">
          <el-select v-model="resource.categoryId" placeholder="全部" clearable style="width: 250px">
            <el-option v-for="item in categoryOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述：">
          <el-input v-model="resource.description" type="textarea" :rows="5" style="width: 250px"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleSave()" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  import {
    formatDate
  } from "@/utils/date";
  import {
    axios
  } from "../../utils";

  //查询条件
  const listQuery = {
    currentPage: 1,
    pageSize: 10,
    name: null,
    url: null,
    categoryId: null
  };

  //资源对象
  const defaultResource = {
    id: null,
    name: null,
    url: null,
    categoryId: null,
    description: ""
  };

  export default {
    name: "resourceList",
    title: "资源管理",
    //数据部分
    data() {
      return {
        listQuery, //查询条件
        total: 0,
        list: [], //资源数据
        cateList: [], //资源分类数据
        listLoading: false,
        dialogVisible: false,
        resource: Object.assign({}, defaultResource),
        isEdit: false,
        categoryOptions: [], //下拉列表
        defaultCategoryId: null //默认分类id
      };
    },

    //钩子函数
    created() {
      //获取资源数据
      this.getResourceList();

      //获取资源分类数据
      this.getResourceCateList();
    },
    methods: {
      //方法1: 获取资源数据
      getResourceList() {
        this.listLoading = true;
        axios.post("/resource/findAllResource", this.listQuery).then(res => {
          this.list = res.data.content.list;
          this.total = res.data.content.total;
          this.listLoading = false;
        });
      },

      //方法2: 获取资源分类数据
      getResourceCateList() {
        axios.get("/resourceCategory/findAllResourceCategory").then(res => {
          //保存资源分类数据
          this.cateList = res.data.content;

          //遍历获取资源分类信息 展示都下拉列表
          for (let i = 0; i < this.cateList.length; i++) {
            //取出分类对象
            const cate = this.cateList[i];

            //保存资源分类的名称和id categoryOptions
            this.categoryOptions.push({
              label: cate.name,
              value: cate.id
            });
          }
        });
      },

      //方法3: 添加资源
      handleAdd() {
        this.dialogVisible = true;
        this.isEdit = false;
        this.resource = Object.assign({}, defaultResource);
      },

      //方法4: 添加&修改资源
      handleSave() {
        axios.post("/resource/saveOrUpdateResource", this.resource).then(res => {
          this.dialogVisible = false;
          this.getResourceList();
        });
      },

      //方法5: 编辑资源
      handleUpdate(row) {
        this.dialogVisible = true;
        this.isEdit = true;
        this.resource = Object.assign({}, row); //回显
      },

      //页码变化触发的函数
      handleCurrentPageChange(page) {
        this.listQuery.currentPage = page;
        this.getResourceList();
      },

      //每页显示条数变化触发的函数
      handlePageSizeChange(size) {
        this.listQuery.pageSize = size;
        this.getResourceList();
      },

      //查询按钮
      handleSearchList() {
        this.getResourceList();
      },

      //删除资源
      handleDelete(row) {

        this.$confirm("是否要删除该资源", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
          .then(() => {
            //请求后台
            axios
              .get("/resource/deleteResource?id=" + row.id)
              .then(res => {
                this.getResourceList();
              })
              .catch(err => {
                this.$message.error("操作失败! ! !");
              });
          })
          .catch(() => {});
      },

      //资源分类管理
      handleShowCategory() {
        this.$router.push({
          path: "/resourceCategory"
        });
      },

      handleResetSearch() {
        this.listQuery.name = null;
        this.listQuery.url = null;
        this.listQuery.categoryId = null;
      }
    },
    filters: {
      formatDateTime(time) {
        if (time == null || time === "") {
          return "N/A";
        }
        const date = new Date(time);
        return formatDate(date, "yyyy-MM-dd hh:mm:ss");
      }
    }
  };
</script>
<style></style>
